package liuds.music.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.List;

import liuds.music.App;
import liuds.music.model.DbHelp;
import liuds.music.model.Music;

/**
 * Created by Administrator on 2018/5/2.
 */

public class MusicUtils {

//    private static List<Music> localMusicList;
    public static List<Music> playMusicList;

    public final static String PLAY_INDEX = "playIndex";
    public final static String PLAY_TYPE = "playType";

    public enum  PlayType{
        RANDOM,ORDERLY,CIRCULATION
    }


    //获取本地所有音乐
    public static List<Music> getLocalMusic(){
        List<Music> list = new ArrayList<Music>();

        Cursor cursor = null;
        try{
            cursor = App.getContext().getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[] { MediaStore.Audio.Media._ID,
                            MediaStore.Audio.Media.DISPLAY_NAME,
                            MediaStore.Audio.Media.TITLE,
                            MediaStore.Audio.Media.DURATION,
                            MediaStore.Audio.Media.ARTIST,
                            MediaStore.Audio.Media.ALBUM,
                            MediaStore.Audio.Media.YEAR,
                            MediaStore.Audio.Media.MIME_TYPE,
                            MediaStore.Audio.Media.SIZE,
                            MediaStore.Audio.Media.DATA },
                    null,
                    null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

            while(cursor.moveToNext()){
                Music music = new Music();

                //ID
                music.setId(cursor.getInt(0));
                // 文件名
                music.setFileName(cursor.getString(1));
                // 歌曲名
                music.setTitle(cursor.getString(2));
                // 时长
                music.setDuration(cursor.getInt(3));
                // 歌手名
                music.setSinger(cursor.getString(4));
                // 专辑名
                music.setAlbum(cursor.getString(5));

                if (cursor.getString(6) != null) {
                    music.setYear(cursor.getString(6));
                } else {
                    music.setYear("未知");
                }
                // 歌曲格式
                music.setType(cursor.getString(7).trim());
                // 文件大小
                if (cursor.getString(8) != null) {
                    float size = cursor.getInt(8) / 1024f / 1024f;
                    music.setSize((size + "").substring(0, 4) + "M");
                } else {
                    music.setSize("未知");
                }
                // 文件路径
                if (cursor.getString(9) != null) {
                    music.setFileUrl(cursor.getString(9));
                }
                list.add(music);
            }
        }
        catch (Exception e){
            Utils.toast("错误:"+e.getMessage());
        }
        finally {
            if(cursor!=null){
                cursor.close();
            }
        }

        return list;
    }

    public static List<Music> getPlayMusicList(){
        if(playMusicList == null){
            if(getPlayType() == PlayType.RANDOM){
                playMusicList = Utils.getRandomList(getLocalMusic());
            }else{
                playMusicList = getLocalMusic();
            }
        }
        return playMusicList;
    }

    public static int getPlayIndex(){
        return App.sp.getInt(PLAY_INDEX,0);
    }

    public static void setPlayIndex(int index){
        App.sp.edit().putInt(PLAY_INDEX,index).commit();
    }

    public static MusicUtils.PlayType getPlayType(){
        int playType = App.sp.getInt(PLAY_TYPE,0);//0:列表循环 1:type_circulation 2:type_random
        switch (playType){
            case 0:
                return MusicUtils.PlayType.ORDERLY;
            case 1:
                return MusicUtils.PlayType.CIRCULATION;
            case 2:
                return MusicUtils.PlayType.RANDOM;
            default:
                return MusicUtils.PlayType.ORDERLY;
        }
    }

    public static void setPlayType(MusicUtils.PlayType playType){
        SharedPreferences.Editor editor = App.sp.edit();
        switch (playType){
            case ORDERLY:
                editor.putInt(PLAY_TYPE,0);
                break;
            case CIRCULATION:
                editor.putInt(PLAY_TYPE,1);
                break;
            case RANDOM:
                editor.putInt(PLAY_TYPE,2);
                break;
        }
        editor.commit();
    }

}
